/*
 * Copyright (c) Eldanichu  DMR.js (2024-9-12).
 */

import { RequestAPI } from '@/utils/RequestAPI'
import { StoreUtils } from '../utils/StoreUtils'
import { defineStore } from 'pinia'
import { StringUtils } from '../utils/StringUtils'
import { ParamUtil } from '../utils/convertable/ParamUtil'
import { ObjectArray } from '../utils/convertable/ObjectArray'
import { CONVERT_TYPE } from '../utils/convertable/Consts'

export const useStockResStore = defineStore(
  'StockResService',
  {
    state(){
      return {

      }
    },
    actions: {
      async getList( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/storage/ver"),
            method: "get",

            params: {
              ...payload
            }
          });

          const { data, msg, code } = res;

          const { Rows = [], Total = 0 } = data || {};

          return {
            Rows,
            Total,
            msg,
            code
          };
        } catch (e) {
          throw new Error(e);
        }
      },
      async getDetail( payload) {
        let api = new RequestAPI()
        const { id } = payload || {};

        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/storage/ver", id),
            method: "get"
          });
          const { data } = res;
          return data;
        } catch (e) {
          throw new Error(e);
        }
      },
      async listMod( payload) {
        let api = new RequestAPI()
        const { act, id, form } = payload || {};
        if (StringUtils.isEmptyOrNull(act)) {
          throw new Error("Modify action is not defined.");
        }

        const url = {
          1: { addr: "/storage/ver", method: "post", id: `` },
          2: { addr: "/storage/ver", method: "post", id: `${id}` }
        };

        const _config = url[act];
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl(_config["addr"]),
            method: _config["method"],

            data: {
              ...form
            }
          });
          const { data, msg, code, success } = res;

          return {
            msg,
            code,
            success
          };
        } catch (e) {
          throw new Error(e);
        }
      },
      async listDelete( payload) {
        let api = new RequestAPI()
        const { ids = [] } = payload || {};
        const ids_str = ids.join(",");
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("storage/ver", ids_str),
            method: "delete"
          });
          const { msg, code } = res;

          return {
            msg,
            code
          };
        } catch (e) {
          throw new Error(e);
        }
      },
      async getStorageSnapshot( payload) {
        const { bwa_id } = payload || {};
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/storage/inv/snapshot"),
            method: "get",
            params: {
              bwaId: bwa_id
            }
          });
          const { data } = res;
          return new ObjectArray(data || [])
            .setType(CONVERT_TYPE.S)
            .excludeTypeKey([
              'bwlName',
              'isiQty',
              'verQty',
            ])
            .build();
        } catch (e) {
          throw new Error(e);
        }
      },
      async getStockWarnList( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/storage/inv/warn"),
            method: "get",

            params: {
              ...payload
            }
          });

          const { data, msg, code } = res;

          const { Rows = [], Total = 0 } = data || {};

          return {
            Rows,
            Total,
            msg,
            code
          };
        } catch (e) {
          throw new Error(e);
        }
      },
      async getStockList( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/storage/inv/page"),
            method: "get",

            params: {
              ...payload
            }
          });

          const { data, msg, code } = res;

          const { Rows = [], Total = 0 } = data || {};

          return {
            Rows,
            Total,
            msg,
            code
          };
        } catch (e) {
          throw new Error(e);
        }
      },
      async adjustAccount( payload = {}) {
        let api = new RequestAPI()
        const {ivoId} = payload
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/storage/ver/account",ivoId),
            method: "get",
          });

          const { data, msg, code,success } = res;


          return {
            data,
            msg,
            code,
            success
          };
        } catch (e) {
          throw new Error(e);
        }
      },
    }
  }
)

